<template>
    <div class="login-container-wrapper">
        <div class="login-container">
            <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-form"
                @submit.prevent="handleSubmit">
                <h2 class="title">欢迎来到知港，请登录</h2>
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" placeholder="用户名" clearable />
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" placeholder="密码" show-password type="password" clearable />
                </el-form-item>
                <el-form-item>
                    <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" native-type="submit" class="login-button" block>
                        登录
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { useUserStore } from "@/store/user"
const router = useRouter();
const userStore = useUserStore();

const loginFormRef = ref(null);

const loginForm = reactive({
    username: localStorage.getItem('username') || '',
    password: "",
    remember: !!localStorage.getItem('username'),
});

const rules = {
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};

const handleSubmit = () => {
    loginFormRef.value.validate(async (valid) => {
        if (!valid) return;
        const res = await userStore.login(loginForm.username, loginForm.password, loginForm.remember);
        if (res) {
            ElMessage.success({
                message: '登录成功',
                duration: 1000,
            });
            setTimeout(() => {
                router.push("/harborSpace");
            }, 1000);

        }
    });
}
</script>

<style scoped lang="less">
.login-container-wrapper {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: var(--bg-color-light);
}

.login-container {
    width: 360px;
    padding: 32px 28px 40px;
    background: var(--bg-color-base);
    box-shadow: 0 6px 18px var(--shadow-base);
    border-radius: 16px;
    box-sizing: border-box;
}

.title {
    text-align: center;
    margin-bottom: 36px;
    font-weight: 700;
    font-size: 22px;
    color: var(--primary-color);
    line-height: 1.3;
    letter-spacing: 0.03em;
}

.el-form-item {
    margin-bottom: 20px;
}

.login-button {
    border-radius: 28px;
    font-size: 16px;
    height: 40px;
    font-weight: 600;
    width: 100%;
    display: block;
}
</style>
